#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H

#include <Globals.h>
#include <Resource.h>
#include <Map.h>

/**
  Syscall interface.
**/
namespace ResourceManager_IF
{
  const int mkfs    =0;
  const int mkbuf   =1;
  const int share   =2;
  const int freebuf =3;
  const int unshare =4;
};

/**
  Manages the calling and distribution of system and user resources
  throughout the system.
**/
class ResourceManager : public Callable
{
public:
  ResourceManager();
  ~ResourceManager();
  
  /**
    Creates and returns a new resource ID, taking a pointer
    to the new resource as an argument.
  **/
  u32int create(Resource *r, const char *name=NULL);
  
  /**
    Returns a resource object for the given ID.
  **/
  Resource *request(u32int id);
  Resource *request(const char *name);

  /**
    Creates a new filesystem.
  **/
  u32int mkfs();


  u32int mkbuf(Address len);
  
  /**
    Frees a buffer created by mkbuf and optionally copies the contents back to a location.
  **/
  void freebuf(u32int so);

  /**
    Similar to mkbuf but intended for smaller allocations (such as passing a string between address
    spaces, for example). This does not enforce memory protection so use at your own risk!
    'ptr' is copied into the new buffer automatically.
  **/
  void *share(void *ptr, u32int len);

  /**
    Removes a buffer created by 'share'. Optionally copies the data back.
  **/
  u32int unshare(void *buf, void *ptr, u32int len);

  /**
    Removes a resource object from the internal map.
  **/
  void destroy(u32int id);
  
  Resource *resource;
  
private:
  typedef Map<u32int,Resource*,73> ResourceIdMap;
  typedef Map<char *,Resource*,73> ResourceNameMap;
  ResourceIdMap resources;
  ResourceNameMap names;
  
  u32int nextId;
};

#endif
